angular.module('Controller')

.controller 'EdNavCtrl', ($http,$rootScope,$scope, $state,commonUtil,menuManage,commonConstant,$localStorage,urls) ->
    loadMenu=()->
        console.log "loadMenu"
        $scope.menus=menuManage.get()
        console.log "$localStorage.firstMenuShow",$localStorage.firstMenuShow
        if $localStorage.firstMenuShow
            $scope.clickMenu(e,$scope.menus[0],true)
            $localStorage.firstMenuShow = false
        id = window.localStorage.currentHash
        if eval(id) == 0
            id =$localStorage.currentHash
        console.log id
        e = undefined
        _.each $scope.menus,(item)->
            if id == item.id
                $scope.clickMenu(e,item,true)
            else
                _.each item.subMenus,(v)->
                    if id == v.id
                        _.delay ->
                            spreadMenu(item)
                        ,1000
                        $scope.clickMenu(e,v,true)
    window.logout=->
        $http.get('@sunagy/Ui/ApiSunagy/logout')
        .success (resp)->
            if resp.code == '000_000'
                window.location.hash = '/login'
        .error (resp)->
            console.log resp
    window.refreshContent=(url)->
        console.log(url)
        $("#modalContent").attr("style", 'display:block;');
        $("#modalContent iframe").attr("src", url);
        $("#ng-app").attr("style", 'overflow:hidden;');
    window.mockMenuClick = ()->
        console.log("mockMenuClick1");
        loadMenu()
    $scope.taskRemainingCount = 0
    $scope.sys_backend = urls.sys_backend
    calPageHeight=(isNew)->
        if isNew then return
        pageHeight=$('.nav-container').get(0)?.offsetHeight
        if pageHeight
            tabHgt=$('#myTab').get(0).offsetHeight
            $('#wf-main-container').css('height',(pageHeight+60)+'px')
            $('#myTabContent').css('height',(pageHeight-tabHgt)+'px')
        ''
    _.defer(()->
        calPageHeight(true)
        $(window).resize(()->
            calPageHeight(true)
        )
    )
    $scope.logout = ->
        window.logout()
#        window.refreshContent('http://www.baidu.com')
    $scope.clickMenu=(event,item,preventStack)->
        if event
            event.preventDefault()
        if item?.subMenus
            spreadMenu(item,preventStack)
            goPage(item,preventStack)
        else
            goPage(item,preventStack)
        document.documentElement.scrollTop = document.body.scrollTop =0
        return true
    spreadMenu=(item)->
        menuId=item.id
        if($('#'+menuId).hasClass('open'))
            item.isOpen = true
            $('#'+menuId).removeClass("open").find("ul").slideUp();
        else
            item.isOpen = false
            $('.nav-menu').removeClass("open").find("ul").slideUp();
            $('#'+menuId).addClass("open").find("ul").slideDown();
    goPage=(item,preventStack)->
        menuManage.route(item,preventStack)
    _.extend $scope,
        customMenu:
            margin:"5px 10px"
            background:commonConstant.topBg
        theme:
            background:commonConstant.pageBg
        fontColor:
            color:commonConstant.pageFontColor
    $rootScope.$on 'shortcutRoute',(obj,e,item)->
        $scope.clickMenu(e,item)
    loadMenu()



